package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.help.UrlReplace;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.net.protocol.LoginCodeProtocol;
import com.example.administrator.zhongyichou.net.protocol.LoginProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/27.
 * 描述：登录
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.txt_request_code)
    TextView txtRequestCode;
    @BindView(R.id.btn_into)
    Button btnInto;
    @BindView(R.id.txt_agreement)
    TextView txtAgreement;
    private CountDownTimer countDownTimer;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initTime();
    }

    @Override
    protected void responseData(GsonBaseProtocol mGsonBaseProtocal) {

    }

    @Override
    protected HttpLoader initHttpLoader() {
        return null;
    }

    @Override
    protected int initContentView() {
        return R.layout.login_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    @OnClick({R.id.txt_request_code, R.id.btn_into, R.id.txt_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_request_code:
                String phone = editName.getText().toString().trim();
                if (phone.length() != 11) {
                    ToastUtil.showToast("手机号码长度不正确");
                    return;
                }
                requestCode(phone);
                break;
            case R.id.btn_into:
                String phone1 = editName.getText().toString().trim();
                if (phone1.length() != 11) {
                    ToastUtil.showToast("手机号码长度不正确");
                    return;
                }
                String code = editCode.getText().toString().trim();
                login(phone1, code);
                break;
            case R.id.txt_agreement:
                break;
        }
    }

    private void login(String phone, String code) {
        Map<String, String> map = new HashMap<>();
        map.put("user_tel", phone);
        map.put("msg_code", code);
        RequestHttpLoader httpLoader = new RequestHttpLoader(LoginProtocol.class);
        httpLoader.setParams(NetConfig.LOGIN, map);
        httpLoader.setResponseUrl(UrlReplace.replace(NetConfig.LOGIN));
        httpLoader.showDialog(this, true, "正在登陆");
        httpLoader.executor(new ResultListener<LoginProtocol>() {
            @Override
            public void success(LoginProtocol mGsonBaseProtocol) {
                MyApplication.putSP(Constants.KEY_HAS_LOGIN, false);
                saveUser(mGsonBaseProtocol);
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    private void saveUser(LoginProtocol loginProtocol) {
        MyApplication.putSP(Constants.USER_ID, loginProtocol.getData().getUser_id());
        MyApplication.putSP(Constants.USER_PHONE, loginProtocol.getData().getUser_tel());
        MyApplication.putSP(Constants.USER_HEAD, loginProtocol.getData().getHead_img());
    }

    /**
     * 获取验证码
     *
     * @param phone
     */
    private void requestCode(String phone) {
        Map<String, String> map = new HashMap<>();
        map.put("user_tel", phone);
        map.put("msg_type", "0");
        RequestHttpLoader httpLoader = new RequestHttpLoader(LoginCodeProtocol.class);
        httpLoader.setParams(NetConfig.GET_CODE, map);
        httpLoader.executor(new ResultListener<LoginCodeProtocol>() {
            @Override
            public void success(LoginCodeProtocol mGsonBaseProtocol) {
                ToastUtil.showToast("验证码" + mGsonBaseProtocol.getData().getMsg_code());
                txtRequestCode.setEnabled(false);
                countDownTimer.start();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }


    private void initTime() {
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                millisUntilFinished = millisUntilFinished / 1000;
                txtRequestCode.setText(Math.round(millisUntilFinished) + "秒");
            }

            @Override
            public void onFinish() {
                txtRequestCode.setText("获取验证码");
                txtRequestCode.setEnabled(true);
            }
        };
    }

}
